home *** CD-ROM | disk | FTP | other *** search
- #
- # File: pclient.pl
- # Author: G. Paul Ziemba
- # Date: 93.01.25
- # SCCS: @(#)pclient.pl 1.8 5/15/93
- # Purpose: proxy ftp client for use with tcprelay
- #
-
- #########################
- # Begin Configuration #
- #########################
-
- #$debug = 1;
- #$verbose = 1;
-
- #########################
- # End Configuration #
- #########################
-
- $| = 1;
-
- #
- # ptelnet [-v] <host> [<port>]
- # pftp [-v] <host> [<port>]
- #
-
- $0 =~ s:^.*/::; # basename
- if ($0 eq "pftp") {
- $Application = $Mode = 'ftp';
- $Service = 'ftp'; # unless port is specified
- } elsif ($0 eq "ptelnet") {
- $Application = 'telnet';
- $Service = 'telnet'; # unless port is specified
- } else {
- print "$0: invalid name for myself\n";
- exit 1;
- }
-
- $server = shift;
- if ($server eq "-v") {
- ++$VerboseMode;
- $server = shift;
- }
- if (!defined($server)) {
- print "usage: $0 [-v] <destination> [<port>]\n";
- exit 1;
- }
-
- if (defined($ARGV[0])) {
- $Service = shift;
- }
-
-
-
- unless (do 'sys/socket.ph') {
- eval 'sub SOCK_STREAM {1;} sub AF_INET {2;} sub PF_INET {2;}';
- }
-
- #
- # Set up relay spec (port + ipaddr)
- #
- ($name, $aliases, $port, $proto) = getservbyname("tcprpm", "tcp");
- ($name, $aliases, $type, $len, $gw_ip) = gethostbyname($RELAYHOST);
- $them = pack('S n a4 x8', &AF_INET, $port, $gw_ip);
-
- #
- # connect to relay
- #
- print "Connecting to relay server $RELAYHOST ...\n" if ($VerboseMode);
- ($name, $aliases, $proto) = getprotobyname('tcp');
- socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
- connect(S, $them) || die "connect: $!";
-
- select(S); $| = 1; select(stdout);
-
-
- #
- # Do it
- #
- print "Requesting relay ...\n" if ($VerboseMode);
- &transact("", '220');
- &transact("server $server", '250');
- &transact("service $Service", '250');
- if (defined($Mode)) {
- &transact("mode $Mode", '250');
- }
- $port = &transact("relay", '212');
-
- $port =~ tr/A-Z/a-z/;
- $port =~ s/^\d\d\d\s+port\s+(\d+)(\s+.*)*$/\1/;
-
- if ($port =~ /^\d+$/) {
- $port = "-$port"
- if (($Application eq "telnet") && ($CHARMODE_PORT eq "-"));
- system("$Application $RELAYHOST $port");
- } else {
- print STDERR "\nRelay protocol error\n";
- exit 1;
- }
- close(S);
- exit 0;
-
-
- sub transact { # $send $expect
- local($send, $expect) = @_[0,1];
- local(@responses) = ();
-
- printf("send[%s] expect[%s]\n", $send, $expect) if $debug;
-
- print S $send."\n" unless ($send eq "");
- while (<S>) {
- if (/^$expect/) {
- print if $verbose;
- print "gotit\n" if $debug;
- return $_;
- }
- if ($VerboseMode) {
- print;
- } else {
- push(@responses, $_);
- }
- last unless (/^\d\d\d-/);
- }
- if (!$VerboseMode) {
- for (@responses) {
- print;
- }
- }
- print "Error, closing connection\n";
- close(S);
- exit 1;
- }
-